package com.seaengine.util
{
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;

	/**
	 * 深拷贝对象
	 * @author 周玉龙
	 * 二〇一一年四月十三日 16:42:50
	 */	
	public class ObjectClone
	{
	
		//--------------------------------------------------------------------------
		//
		//  variable
		//
		//--------------------------------------------------------------------------

		public function ObjectClone()
		{
			
		}
	

		//--------------------------------------------------------------------------
		//
		//  static
		//
		//--------------------------------------------------------------------------
		/**
		 * 克隆对象(除显示对象)
		 * @param targetObj  目标对象
		 */		
		public static function clone(targetObj:Object):Object
		{
			var byteArray:ByteArray = new ByteArray();
			byteArray.writeObject(targetObj);
			byteArray.position = 0;
			var obj:Object = byteArray.readObject();
			return obj;
		}
		
		public static function duplicateMovieClip(source:MovieClip, isAdd:Boolean = false):MovieClip 
		{
         	//调用源对象类的构造函数
       		var tclass:Class = Object(source).constructor;
	       	var copy:MovieClip = new tclass();        
	        //对有差异的属性进行复制。
	         copy.transform = source.transform;
	         copy.filters = source.filters;
	         copy.cacheAsBitmap = source.cacheAsBitmap;
	         copy.opaqueBackground = source.opaqueBackground;
	         if (source.scale9Grid) {
	             var rect:Rectangle = source.scale9Grid;
	             //这个据说是flash9中scale9Grid的一个bug.
	             rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
	             copy.scale9Grid = rect;         }
	        
	         //是否自动添加到与源对象相同的父对象中。
	         if (isAdd && source.parent) {
	             source.parent.addChild(copy);
	         }
	         return copy;
	 	}
	}
}